//
//  LoginViewController.m
//  SinceLe
//
//  Created by 李祎 on 15/11/12.
//  Copyright © 2015年 李元喜. All rights reserved.
//

#import <AVOSCloud.h>
#import "LoginViewController.h"
#import "CheckUserLogin.h"
#import "SignupViewController.h"
#import "UserMessageTool.h"

@interface LoginViewController ()

@property (strong, nonatomic) IBOutlet UITextField *accountText;

@property (strong, nonatomic) IBOutlet UITextField *passwordText;

- (IBAction)loginButtonClicked:(UIButton *)sender;

- (IBAction)registerButtonClicked:(UIButton *)sender;

- (IBAction)forgetButtonClicked:(UIButton *)sender;

- (IBAction)backButtonClicked:(UIButton *)sender;


@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)loginButtonClicked:(UIButton *)sender {
    //点击登录
//    AVQuery* query = [AVQuery queryWithClassName:@"_User"];
//    [query whereKey:@"mobilePhoneNumber" equalTo:_accountText.text];
//    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
//        if (!error) {
//            NSLog(@"========= %lu",(unsigned long)objects.count);
//        }
//    }];
    [AVUser logInWithMobilePhoneNumberInBackground:_accountText.text password:_passwordText.text block:^(AVUser *user, NSError *error) {
        if (!error) {
            //登录成功
            //保存登录状态
            [CheckUserLogin userIsLogin];
            //保存用户信息
            [UserMessageTool saveName:user.username];
            
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            NSLog(@"===========%@",[error.userInfo objectForKey:@"error"]);
            if ([[error.userInfo objectForKey:@"error"] isEqualToString:@"Could not find user"]) {
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户不存在!" preferredStyle:UIAlertControllerStyleAlert];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    [alert dismissViewControllerAnimated:YES completion:nil];
                }]];
                
                [self presentViewController:alert animated:YES completion:^{
                    
                }];
                
            }else if ([[error.userInfo objectForKey:@"error"] isEqualToString:@"The username and password mismatch."]){
                UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"账号／密码不匹配!" preferredStyle:UIAlertControllerStyleAlert];
                
                [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    [alert dismissViewControllerAnimated:YES completion:nil];
                }]];
                
                [self presentViewController:alert animated:YES completion:^{
                    
                }];

            }
        }
    }];
    
}

- (IBAction)registerButtonClicked:(UIButton *)sender {
    UIViewController* vc = [[SignupViewController alloc] initWithNibName:@"SignupViewController" bundle:nil];
    [self.navigationController pushViewController:vc animated:YES];
}

- (IBAction)forgetButtonClicked:(UIButton *)sender {
}

- (IBAction)backButtonClicked:(UIButton *)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}
@end
